<?php
namespace Mouta\AmistateBundle\utils;

use Mouta\AmistateBundle\utils\solr\Apache_Solr_Document;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_Exception;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransportException;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransport_Abstract;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransport_Curl;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransport_CurlNoReuse;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransport_FileGetContents;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransport_Interface;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_HttpTransport_Response;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_InvalidArgumentException;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_ParserException;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_Response;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_Service;
use Mouta\AmistateBundle\utils\solr\Apache_Solr_Service_Balancer;

class SfSolr{

    public static function getQuery($query){
        $parameters = parse_ini_file( __DIR__.'/../../../../app/config/parameters.ini');
        $url = 'http://'.$parameters['solr_server'].':'.$parameters['solr_port'].'/'.$parameters['solr_app'].'/select/' . $query."&wt=phps";
        $r = file_get_contents($url);
        return unserialize($r);
    }
    
    public function AddOrUpdateUser(\Mouta\AmistateBundle\Entity\Usuario $user){
        $parameters = parse_ini_file( __DIR__.'/../../../../app/config/parameters.ini');
        $solr = new Apache_Solr_Service($parameters['solr_server'],$parameters['solr_port'],'/'.$parameters['solr_app'].'/');
        $document = new Apache_Solr_Document();
        $document->addField('id', $user->getId());
        $document->addField('nombre', $user->getNombre());
        $document->addField('para', $user->getPara());
        $document->addField('cumple', $user->getCumple()->format('Y-m-d')."T23:59:59Z");
        $document->addField('ultima_actividad', $user->getCreatedAt()->format('Y-m-d')."T23:59:59Z");
        $document->addField('descripcion', $user->getDescripcion());
        $document->addField('sexo', $user->getSexo());
        $document->addField('busco', $user->getBusco());
        $document->addField('pais', $user->getPais());
        $document->addField('provincia', $user->getProvincia());
        $document->addField('ciudad', $user->getCiudad());
        $document->addField('puntos', $user->getPuntos());
        $document->addField('votos', $user->getVotos());
        if( $user->getLongitude() )
            $document->addField('coordenadas', $user->getLongitude().','.$user->getLatitude());
        
        foreach($user->getTags() as $tag)
        {
            $document->addField('tags', $tag->getTagGoogle());
        }
        $document->addField('tags', $user->getSeoUbicacion());
        
        $imagenes = $user->getImagenes();
        $imagendefa = "";
        foreach($imagenes as $imagen){
            if($imagen->getDefa())
                $imagendefa = $imagen->getImagenBig();
        }
        $document->addField('fotodefault', $imagendefa);
        if($imagendefa)
            $document->addField('tienefoto', 1);
        else
            $document->addField('tienefoto', 0);
        
        $solr->addDocument($document);
        $solr->commit();
    }
    
    public function DeleteUser(\Mouta\AmistateBundle\Entity\Usuario $user){
        $parameters = parse_ini_file( __DIR__.'/../../../../app/config/parameters.ini');
        $solr = new Apache_Solr_Service($parameters['solr_server'],$parameters['solr_port'],'/'.$parameters['solr_app'].'/');
        $solr->deleteById($user->getId());
        $solr->commit();
    }
}